
package hellscreamlegacy;
import java.sql.*;

public class Persona {
   
 Conectate con;
  
  public Persona (){
    con = new Conectate();
  } 
   
    public void NuevaPersona(String name, String ap, String am, String mail){
       try {            
            PreparedStatement pstm = con.getConnection().prepareStatement("insert into " + 
                    "persona(Nombre, appPaterno, appMaterno, mail) " +
                    " values(?,?,?,?)");            
            pstm.setString(1, name);
            pstm.setString(2, ap);
            pstm.setString(3, am);                        
            pstm.setString(4, mail);                        
            pstm.execute();
            pstm.close();            
         }catch(SQLException e){
         System.out.println(e);
      }
   }

  public Object [][] getDatos(){
      int x = 0;
      //obtenemos la cantidad de registros existentes en la tabla
      try{         
         PreparedStatement pstm = con.getConnection().prepareStatement("SELECT count(1) as total FROM persona ");
         ResultSet res = pstm.executeQuery();
         res.next();
         x = res.getInt("total");
         res.close();
      }catch(SQLException e){
         System.out.println(e);
      }
      
    Object[][] s = new String[x][2];  
    //realizamos la consulta sql y llenamos los datos en "Object"
      try{    
         PreparedStatement pstm = con.getConnection().prepareStatement("SELECT " +
            " id, Nombre " +
            " FROM persona" +
            " ORDER BY id ");
         ResultSet res = pstm.executeQuery();
         int i = 0;
         while(res.next()){
            String estCodigo = res.getString("id");
            String estNombre = res.getString("nombre");
            s[i][0] = estCodigo;            
            s[i][1] = estNombre;            
            i++;
         }
         res.close();
          }catch(SQLException e){
         System.out.println(e);
    }
    return s;
 }
    

  
    
}
